Skip to content

Method: mapAssertions(AxiomDescriptor, Map, Map)

1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
21: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
22: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
23: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
24: import cz.cvut.kbss.ontodriver.model.Assertion;
25: import cz.cvut.kbss.ontodriver.model.Axiom;
26: import org.apache.jena.rdf.model.Property;
27: import org.apache.jena.rdf.model.RDFNode;
28: import org.apache.jena.rdf.model.Resource;
29: import org.apache.jena.rdf.model.ResourceFactory;
30:
31: import java.net.URI;
32: import java.util.Collection;
33: import java.util.HashMap;
34: import java.util.Map;
35: import java.util.Set;
36:
37: class MainAxiomLoader {
38:
39: private final AbstractAxiomLoader inferredLoader;
40: private final ExplicitAxiomLoader explicitLoader;
41:
42: MainAxiomLoader(StorageConnector connector, InferredStorageConnector inferredConnector) {
43: this.explicitLoader = new ExplicitAxiomLoader(connector);
44: // It is possible that the inferred connector is null - if we are using the read_committed strategy or only snapshot,
45: // without inference
46: this.inferredLoader = new InferredAxiomLoader(inferredConnector);
47: }
48:
49: /**
50: * Checks whether the storage contains the specified axiom.
51: *
52: * @param axiom Axiom whose existence should be verified
53: * @param contexts Contexts to search, optional (empty indicates the default context)
54: * @return {@code true} if the axiom exists, {@code false} otherwise
55: */
56: boolean contains(Axiom<?> axiom, Set<URI> contexts) {
57: return axiom.getAssertion().isInferred() ? inferredLoader.contains(axiom, contexts) :
58: explicitLoader.contains(axiom, contexts);
59: }
60:
61: /**
62: * Checks whether the storage inferred the specified axiom.
63: * <p>
64: * Note that given the nature of the Jena API, this method will return {@code false} even if the statement is both
65: * asserted and inferred, as there is no way to easily ask only for inferred statements but both asserted and
66: * inferred statements are returned.
67: * <p>
68: * Also note that if the repository does not contain the statement at all, {@code false} is returned.
69: *
70: * @param axiom Axiom whose inference to check
71: * @param contexts Contexts to search, optional (empty indicates the default context)
72: * @return iff the specified statement is inferred in any of the specified contexts
73: */
74: boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
75: final Resource subject = ResourceFactory.createResource(axiom.getSubject().getIdentifier().toString());
76: final Property property = ResourceFactory.createProperty(axiom.getAssertion().getIdentifier().toString());
77: final RDFNode object = JenaUtils.valueToRdfNode(axiom.getAssertion(), axiom.getValue());
78: return inferredLoader.contains(subject, property, object, contexts) && !explicitLoader.contains(subject, property, object, contexts);
79: }
80:
81: /**
82: * Loads axioms corresponding to the specified descriptor.
83: *
84: * @param descriptor Descriptor of axioms to load
85: * @return Matching axioms
86: */
87: Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
88: final Map<String, Assertion> asserted = new HashMap<>(descriptor.getAssertions().size());
89: final Map<String, Assertion> inferred = new HashMap<>(descriptor.getAssertions().size());
90: mapAssertions(descriptor, asserted, inferred);
91: final Collection<Axiom<?>> result = explicitLoader.find(descriptor, asserted);
92: result.addAll(inferredLoader.find(descriptor, inferred));
93: return result;
94: }
95:
96: private static void mapAssertions(AxiomDescriptor descriptor, Map<String, Assertion> asserted,
97: Map<String, Assertion> inferred) {
98:• for (Assertion a : descriptor.getAssertions()) {
99:• if (a.isInferred()) {
100: inferred.put(a.getIdentifier().toString(), a);
101: } else {
102: asserted.put(a.getIdentifier().toString(), a);
103: }
104: }
105: }
106: }